<template>
  <div>
    <a-form-model :model="form" ref="userDept">
      <a-form-model-item prop="deptIds">
        <a-transfer
          :dataSource="deptList"
          v-model="form.deptIds"
          :listStyle="{width: '45%',height: '480px'}"
          :titles="['选择部门', '已选择部门']"
          :render="item=>item.title"
          :target-keys="form.deptIds"
          @change="handleTransferChange"
          filterable>
        </a-transfer>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
  export default {
    name: 'UserDept',
    components: {},
    data() {

      return {
        deptList: [],
        form: {
          userId: '',
          organizationId:'',
          deptIds: []
        }
      }
    },
    methods: {
      setData(data) {
        if (data && data.userId) {
          this.form.userId = data.userId
          this.form.organizationId = data.organizationId
          this.getAuthDeptList(data.userId,data.organizationId)
        }
      },
      handleSubmit() {
        this.$refs['userDept'].validate(valid => {
          if (valid) {
            this.http.post(this, {
              url: this.api.managerUser.setDept,
              data: {
                userId: this.form.userId,
                deptIds: this.form.deptIds.join(',')
              },
              success: function(me) {
                //me.$emit('handleClose')
              }
            })
          } else {
            return false
          }
        })
      },
      getAuthDeptList(userId,organizationId) {
        this.http.get(this, {
          url: this.api.managerUser.getAuthDeptList,
          data: {
            userId: userId,
            organizationId: organizationId
          },
          success: (me, data) => {
            data.deptList.map(item => {
              item.key = item.deptId
              item.title = item.deptName
            })
            me.deptList = data.deptList
            me.form.deptIds = data.deptIds
          }
        })
      },
      handleTransferChange(targetKeys, direction, moveKeys) {
        this.form.deptIds = targetKeys
      }
    },
    mounted: function() {
    }
  }
</script>
